var enable = true;
var imgurl;
chrome.browserAction.setBadgeBackgroundColor({color:[0, 0, 0, 130]});
if (typeof localStorage.rules == 'undefined') { localStorage.rules = ''; }
var rules = localStorage.rules.split(',');
console.log(rules);

function imageOnClick(info, tab) { 
//	  console.log("item " + info.menuItemId + " was clicked");
//    console.log("info: " + JSON.stringify(info));
//    console.log("tab: " + JSON.stringify(tab));
    imgurl = info.srcUrl;
//    FormAdd = window.open("FromAdd.htm", "FormAdd", "left=100, top=100,width=300, height=150,resizable=no ");
//    FormAdd.document.getElementById('textarea').value = info.srcUrl;
    chrome.windows.create({ url: 'FormAdd.htm', left:100, top:100, width:300, height:200, type:'popup', focused:true }, function(window) {
//		console.log("window: " + JSON.stringify(window));		
    });
}

var image = chrome.contextMenus.create({"title": "拦截图片", "contexts":["image"], "onclick": imageOnClick });

chrome.browserAction.onClicked.addListener(function(tab) {
    //enable = enable ? false : true;
	enable = !enable;
    console.info(enable);
    switchADB();
});

function bcount(request, sender, sendResponse) {
    chrome.browserAction.setBadgeText({ text: request.n });
}

function switchADB() {
    if (enable) {
        chrome.browserAction.setIcon({ path: 'icon.png' });
        chrome.browserAction.setTitle({ title:'开启ADB' });        
        chrome.tabs.onActivated.addListener(loadBlock);
        chrome.tabs.onUpdated.addListener(loadBlock);
		chrome.extension.onMessage.addListener(bcount);
    } else {
        chrome.browserAction.setIcon({ path:'disable.png' });
        chrome.browserAction.setBadgeText({ text:'' });
        chrome.browserAction.setTitle({ title:'禁用ADB' });
		chrome.webRequest.onBeforeRequest.removeListener();
        //chrome.webRequest.onHeadersReceived.removeListener();
        chrome.extension.onMessage.removeListener(bcount);
        chrome.tabs.onActivated.removeListener(loadBlock);
        chrome.tabs.onUpdated.removeListener(loadBlock);
    }
}

function loadBlock(activeInfo) {
    chrome.webRequest.onBeforeSendHeaders.addListener(
        function (details) {
            console.log(details);
            for (i = 0; i < details.requestHeaders.length; ++i) {
                if (details.requestHeaders[i].name === 'User-Agent') {
                    details.requestHeaders[i].value = 'Windows';
                    console.log(details.requestHeaders[i].value);
                    break;
                }
            }
            return {
                requestHeaders: details.requestHeaders
            };},
        { urls: [ "*://*.pcpop.com/*" ]},
        ["blocking", "requestHeaders"]
    );

	chrome.webRequest.onBeforeRequest.addListener(
                    function(details) { return { cancel: true }; },
                    { urls: rules },
                    ["blocking"]);

	chrome.tabs.executeScript(null, { file: 'content.js' });
}

chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    if (request.method == "getLocalStorage")
    	sendResponse({data: localStorage[request.key]});
    if (request.method == "loadBlock"){
		loadBlock;
		sendResponse({});
	}
});

switchADB();
